package com.xingshang.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xingshang.mapper.FavoriteSongMapper;
import com.xingshang.pojo.FavoriteSong;
import com.xingshang.pojo.Song;
import com.xingshang.service.FavoriteSongService;
import com.xingshang.utils.JwtHelper;
import com.xingshang.utils.Result;
import com.xingshang.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 87547
 * @description 针对表【favorite_song】的数据库操作Service实现
 * @createDate 2024-12-17 17:12:02
 */
@Service
public class FavoriteSongServiceImpl extends ServiceImpl<FavoriteSongMapper, FavoriteSong>
        implements FavoriteSongService {

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private FavoriteSongMapper favoriteSongMapper;

    @Override
    public Result addLike(String token, FavoriteSong favoriteSong) {
        Long userId = jwtHelper.getUserId(token);

        String albummid = favoriteSong.getAlbumMid();
        String mid = favoriteSong.getMid();

        QueryWrapper<FavoriteSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("mid", mid);
        queryWrapper.eq("album_Mid", albummid);
        FavoriteSong favoriteSong1 = favoriteSongMapper.selectOne(queryWrapper);
        if (favoriteSong1 != null) {
            Integer isLike = favoriteSong1.getIsLike();
            isLike = 1 - isLike;
            favoriteSong1.setIsLike(isLike);
        } else {
            favoriteSong.setUserId(Math.toIntExact(userId));
            favoriteSong1 = favoriteSong;
        }
        boolean b = favoriteSongMapper.insertOrUpdate(favoriteSong1);
        return Result.ok(b);


    }

    @Override
    public Result getLikeSongByMid(String token, String mid) {
        Long userId = jwtHelper.getUserId(token);
        QueryWrapper<FavoriteSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("mid", mid);
        FavoriteSong favoriteSong = favoriteSongMapper.selectOne(queryWrapper);
        if (favoriteSong != null) {
            return Result.ok(favoriteSong);
        }
        return Result.build(null, ResultCodeEnum.NOT_LIKE);
    }

    @Override
    public Result getLikeSongList(String token) {
        Long userId = jwtHelper.getUserId(token);
        QueryWrapper<FavoriteSong> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_like", 1);
        List<FavoriteSong> favoriteSongs = favoriteSongMapper.selectList(queryWrapper);
        System.out.println(favoriteSongs);
        List<Song> songList = new ArrayList<>();
        List<List<String>> songsList = new ArrayList<>();
        ObjectMapper mapper = new ObjectMapper();
        for (FavoriteSong favoriteSong : favoriteSongs) {
            String mid = favoriteSong.getMid();
            List<String> songMid = new ArrayList<>();
            songMid.add(mid);
            songsList.add(songMid);
        }
        try {
            String jsonData = mapper.writeValueAsString(Collections.singletonMap("songs", songsList));
            HttpResponse response1 = HttpRequest.post("https://qq-music-api-main.vercel.app/batchGetSongInfo")
                    .header("Content-Type", "application/json")  // 设置 Content-Type 为 application/json
                    .body(jsonData)
                    .execute();
            if (response1.getStatus() == 200) {
                String resp1 = response1.body();
                JsonNode jsonNode1 = mapper.readTree(resp1);
                List<JsonNode> trackInfo = jsonNode1.findValues("track_info");
                List<Song> songs = new ArrayList<>();
                for (JsonNode node : trackInfo) {
                    Song song = new Song();
                    song.setMid(node.get("mid").asText());
                    song.setTitle(node.get("title").asText());
                    song.setAlbumMid(node.get("album").get("mid").asText());
                    song.setSingerName(node.get("singer").get(0).get("name").asText());
                    song.setCover("https://y.qq.com/music/photo_new/T002R300x300M000" + song.getAlbumMid() + ".jpg?max_age=2592000");
                    song.setSongId(node.get("id").asLong());
                    songList.add(song);
                }
                return Result.ok(songList);

            }
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return Result.build(null, ResultCodeEnum.NULL_DATA);


    }
}




